package com.yuncheng.oaApi.controller;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.oaApi.constant.OaBusinessConstant;
import com.yuncheng.oaApi.utils.GetUuIdUtils;
import com.yuncheng.spcyApi.utils.MyExcelUtils;
import com.yuncheng.utils.ThreadContextHelper;
import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.common.query.QueryBuilder;
import com.yuncheng.annotation.AutoLog;
import com.yuncheng.utils.I18nUtil;
import com.yuncheng.common.base.controller.BaseController;
import com.yuncheng.oaApi.entity.OaXzswspEntity;
import com.yuncheng.oaApi.service.IOaXzswspEntityService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import utils.DateConversionUtils;

/**
 * @Description: 行政事务审批
 * @Author: yuncheng
 * @Date: 2023-08-01
 * @Version: V1.0
 */
@Api(tags = "行政事务审批接口")
@RestController
@RequestMapping("/api/spzx-oa/oaXzswsp")
public class OaXzswspController extends BaseController<OaXzswspEntity, IOaXzswspEntityService> {

    private static final Logger log = LoggerFactory.getLogger(OaXzswspController.class);

    @Resource
    private IOaXzswspEntityService oaXzswspService;

    @Resource
    private MyExcelUtils oaMyExcelUtils;

    /**
     * 分页列表查询
     *
     * @param oaXzswspEntity
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "行政事务审批-分页列表查询")
    @ApiOperation(value = "行政事务审批-分页列表查询")
    @GetMapping(value = "/list")
    public HttpResult<?> queryPageList(OaXzswspEntity oaXzswspEntity, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        QueryWrapper<OaXzswspEntity> queryWrapper = QueryBuilder.initQueryWrapper(oaXzswspEntity, req.getParameterMap());
        Page<OaXzswspEntity> page = new Page<>(pageNo, pageSize);
        IPage<OaXzswspEntity> pageList = oaXzswspService.page(page, queryWrapper);
        for (OaXzswspEntity vo : pageList.getRecords()) {
            formatValue(vo);
        }
        return HttpResult.ok(pageList);
    }

    /**
     * 编辑
     *
     * @param oaXzswspEntity
     * @return
     */
    @AutoLog(value = "宣传信息审批管理-编辑")
    @ApiOperation(value = "宣传信息审批管理-编辑")
    //@RequiresPermissions("XCXXSP:OaXcxxspgl:edit")
    @PutMapping(value = "/edit")
    public HttpResult<?> edit(@RequestBody OaXzswspEntity oaXzswspEntity) {
        oaXzswspService.updateById(oaXzswspEntity);
        return HttpResult.ok(I18nUtil.message("update.success"));
    }

    /**
     * 添加
     *
     * @param oaXzswspEntity
     * @return
     */
    @AutoLog(value = "宣传信息审批管理-添加")
    @ApiOperation(value = "宣传信息审批管理-添加")
    //@RequiresPermissions("XCXXSP:OaXcxxspgl:add")
    @PostMapping(value = "/add")
    public HttpResult<?> add(@RequestBody OaXzswspEntity oaXzswspEntity) {
        CurrentUserVo currentUser = ThreadContextHelper.getCurrentUser();
        if (StringUtils.isBlank(oaXzswspEntity.getId())){
            oaXzswspEntity.setId(GetUuIdUtils.ReplaceUuId());
        }
        if (StringUtils.isBlank(oaXzswspEntity.getFks())){
            oaXzswspEntity.setFks(currentUser.getDeptName());
        }
        if (StringUtils.isBlank(oaXzswspEntity.getFnbr())){
            oaXzswspEntity.setFnbr(currentUser.getName());
        }
        if (StringUtils.isBlank(oaXzswspEntity.getFuserid())){
            oaXzswspEntity.setFuserid(currentUser.getId());
        }
        if (StringUtils.isBlank(oaXzswspEntity.getFrq())){
            oaXzswspEntity.setFrq(DateConversionUtils.DateToString(new Date()));
        }
        if (StringUtils.isBlank(oaXzswspEntity.getFnbsj())){
            oaXzswspEntity.setFrq(DateConversionUtils.DateToYMDMM(new Date()));
        }
        if (StringUtils.isBlank(oaXzswspEntity.getFsftgdwld())){
            oaXzswspEntity.setFsftgdwld("否");
        }
        oaXzswspService.save(oaXzswspEntity);
        return HttpResult.ok(oaXzswspEntity);
    }


    /**
     * 数据转换
     *
     * @param oaXzswspEntity
     */
    private void formatValue(OaXzswspEntity oaXzswspEntity) {
    }

    @AutoLog(value = "导出行政事务审批数据-表格excel")
    @ApiOperation(value = "导出行政事务审批数据-表格excel")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "ids",value = "主键id字符逗号分割",required = true)
    })
    @GetMapping("/exportxzswData")
    public void exportxzswData(@RequestParam(value = "ids",defaultValue = "")String ids,
                               OaXzswspEntity ndkpBasic,
                               HttpServletResponse response){
        try {
            String name = "exportXxpxData";
            response.setContentType("application/ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename="
                    .concat(String.valueOf(URLEncoder.encode(name+".xls", "UTF-8"))));
            OutputStream out = response.getOutputStream();

            List<String> IdsList = Arrays.asList(ids.split(","));
            List<Object> list = new ArrayList<>(); // 导出的数据
            // 多选导出
            if (StringUtils.isNotEmpty(ids)) {
                List<OaXzswspEntity> ndkpBasicList = oaXzswspService.listByIds(IdsList);
                if (CollectionUtils.isNotEmpty(ndkpBasicList)){
                    for (OaXzswspEntity e : ndkpBasicList){
                        list.add(e);
                    }
                }
            }else {
                LambdaQueryWrapper<OaXzswspEntity> queryWrapper = new LambdaQueryWrapper<>();
                //科室
                queryWrapper.eq(StringUtils.isNotBlank(ndkpBasic.getFks()), OaXzswspEntity::getFks, ndkpBasic.getFks());
                //日期
                queryWrapper.like(StringUtils.isNotEmpty(ndkpBasic.getFrq()), OaXzswspEntity::getFrq,ndkpBasic.getFrq());
                //事项
                queryWrapper.like(StringUtils.isNotEmpty(ndkpBasic.getFsx()), OaXzswspEntity::getFsx,ndkpBasic.getFsx());
                //类别
                queryWrapper.like(StringUtils.isNotEmpty(ndkpBasic.getFlb()), OaXzswspEntity::getFlb,ndkpBasic.getFlb());
                //拟办人
                queryWrapper.like(StringUtils.isNotEmpty(ndkpBasic.getFnbr()), OaXzswspEntity::getFnbr,ndkpBasic.getFnbr());

                if (ndkpBasic.getFrq() != null){
                    queryWrapper.le(OaXzswspEntity::getFrq,ndkpBasic.getFrq());
                }
                if (ndkpBasic.getFnbr() != null){
                    queryWrapper.ge(OaXzswspEntity::getFnbr,ndkpBasic.getFnbr());
                }
                List<OaXzswspEntity> ndkpBasicList = oaXzswspService.list(queryWrapper);
                // 条件导出
                if (CollectionUtils.isNotEmpty(ndkpBasicList)){
                    for (OaXzswspEntity e : ndkpBasicList){
                        list.add(e);
                    }
                }
            }
            oaMyExcelUtils.outputListToExclFile(OaBusinessConstant.IMPORT_OA_XZSWSP_XML,list,out); // 导出模板数据成excel
        } catch (Exception e) {
            log.error("导出行政事务审批数据出错-{}",e.getMessage());
        }
    }

}
